package com.zm.devmag.back.web;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zm.devmag.back.vo.Admin;
import com.zm.devmag.base.init.Initialize;
import com.zm.devmag.base.vo.ResponseMsg;
import com.zm.devmag.user.po.Users;
import com.zm.devmag.user.server.UsersServer;
import com.zm.devmag.util.EncoderHandler;
import com.zm.devmag.util.MD5Util;
import com.zm.devmag.util.RandomUtil;

@RestController
public class LoginController {
	
	@Autowired
	private Initialize init;
	@Autowired
	private UsersServer userService;
	/**
	 * 后台登陆页面
	 * @return
	 */
	@RequestMapping("/loginUi")
	public ModelAndView loginUi(){
		return new ModelAndView("back/loginUi");
	}
	/**
	 * 验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/validCode")
	public void validCode(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setDateHeader("Expires", -1);
		response.setHeader("Cache-Control", "no-Cache");
		response.setHeader("pragma", "no-Cache");
		int height = 25;
		int width = 65;
		BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g =  (Graphics2D) image.getGraphics();
		g.setColor(new Color(221,227,236));
		g.fillRect(0, 0, width, height);
		
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, width-1, height-1);
		
		g.setColor(Color.RED);
		for(int i = 0;i<5;i++){
		    g.drawLine(RandomUtil.getRandInt(0,width), RandomUtil.getRandInt(0,height), RandomUtil.getRandInt(0,width), RandomUtil.getRandInt(0,height));
		}
		String base = "23456789abcdefghjkmnpqrstuvwxyz";
		g.setFont(new Font("宋体",Font.BOLD,18));
		g.setColor(Color.BLACK);
		StringBuffer buffer = new StringBuffer();
		for(int i=0;i<4;i++){
		    String s = base.charAt(RandomUtil.getRandInt(0,base.length()-1))+"";
		    buffer.append(s);
		    g.drawString(s, 2+width/4*i, 20);
		
		}
		System.out.println(buffer.toString());
		request.getSession().setAttribute("validCode", buffer.toString());
		g.dispose();
		ImageIO.write(image, "jpg", response.getOutputStream());
	}
	@ResponseBody
	@RequestMapping(value="/login",method = RequestMethod.POST)
	public Object login(@RequestBody Admin admin,HttpServletRequest request){
		String passCode = (String) request.getSession().getAttribute("validCode");
		/*Admin sysAdmin = (Admin)init.model.get("admin");*/
		if(passCode == null || !passCode.equals(admin.getValidCode())){
			return new ResponseMsg("error","验证码不正确！");
		}
		Users user = userService.SelectUserByUacc(admin.getName());
		if(user == null){
			return new ResponseMsg("error","用户名不正确！");
		}
		System.out.println("登录密码admin.getPassword():"+admin.getPassword());
		String pw = MD5Util.Encode_B64_MD5(admin.getPassword());
		System.out.println("登录密码:"+pw);
		if(!pw.equals(user.getPassw())){
			return new ResponseMsg("error","密码不正确！");
		}
		admin.setUtype(user.getUtype());
		request.getSession().setAttribute("admin", admin);
		return new ResponseMsg("success","登陆成功！");
	/*	else if(!"admin".equals(admin.getName())){
			return new ResponseMsg("error","用户名不正确！");
		}else if(!sysAdmin.getPassword().equals(admin.getPassword())){
			return new ResponseMsg("error","密码不正确！");
		}else{
			request.getSession().setAttribute("admin", admin);
			return new ResponseMsg("success","登陆成功！");
		}	*/	
		
	}
	@RequestMapping(value="/back/logout")
	public ModelAndView logout(HttpServletRequest request){
		
		request.getSession().removeAttribute("admin");
		return new ModelAndView("redirect:/loginUi.do");
	}
	
	
}
